package com.mysite.angular.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mysite.angular.common.BaseController;
import com.mysite.angular.common.Constants;
import com.mysite.angular.common.ResultVo;
import com.mysite.angular.po.AcOperator;
import com.mysite.angular.service.FuncService;
import com.mysite.angular.service.MenuService;
import com.mysite.angular.service.OperatorService;
import com.mysite.angular.vo.AcMenuVo;

@Controller
public class LoginController extends BaseController {

	@Autowired
	private OperatorService operatorService;
	@Autowired
	private FuncService funcService;
	@Autowired
	private MenuService menuService;

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> login(@RequestBody AcOperator operator, HttpSession session) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Integer id = operatorService.checkOperatorExisted(operator);
		if(id != null) {
			resultMap.put("result", id);
			List<String> urls = funcService.getOperatorFunctions(id);
			session.setAttribute(Constants.CURRENT_OPERATOR_FUNCTIONS, urls);
			session.setAttribute(Constants.CURRENT_OPERATOR_ID, id);
			session.setAttribute(Constants.CURRENT_OPERATOR_CODE, operator.getUsercode());
		}
		return resultMap;
	}
	
	@RequestMapping(value = "/menus", method = RequestMethod.GET)
	@ResponseBody
	public Object getMenus(HttpSession session) {
		Integer operatorId = (Integer) session.getAttribute(Constants.CURRENT_OPERATOR_FUNCTIONS);
		operatorId = 1;
		List<AcMenuVo> menus = menuService.getOperatorMenus(operatorId);
		return menus;
	}
}
